package com.tsxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsxy.Exception.MyException;
import com.tsxy.common.Constants;
import com.tsxy.controller.dto.UserDto;
import com.tsxy.entity.Menu;
import com.tsxy.entity.User;
import com.tsxy.mapper.RoleMapper;
import com.tsxy.mapper.RoleMenuMapper;
import com.tsxy.mapper.UserMapper;
import com.tsxy.service.IMenuService;
import com.tsxy.service.IUserService;
import com.tsxy.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 飞
 * @since 2022-02-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private static final Log log = Log.get();

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private IMenuService menuService;

    @Override
    public UserDto login(UserDto userDto) {
        User user = getUserInfo(userDto);
        if (user != null){
            BeanUtil.copyProperties(user,userDto,true);
            String token = TokenUtil.genToken(user.getId(), user.getPassword());
            userDto.setToken(token);

            String role = user.getRole();

            Integer roleId = roleMapper.selectByFlag(role);

            List<Menu> roleMenus = getRoleMenus(roleId);
            userDto.setMenus(roleMenus);
            return userDto;
        }else{
            throw new MyException(Constants.CODE_600,"用户名或密码错误");
        }
    }

    @Override
    public User register(UserDto userDto) {
        User user = getUserInfo(userDto);
        if (user == null){
            user = new User();
            BeanUtil.copyProperties(userDto,user,true);
            boolean save = save(user);// 存入数据库
        }else{
            throw new MyException(Constants.CODE_600,"用户已经存在");
        }
        return user;
    }

    @Override
    public User getByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = getOne(queryWrapper);
        return user;
    }

    private User getUserInfo(UserDto userDto){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDto.getUsername());
        queryWrapper.eq("password",userDto.getPassword());
        User user;
        try {
            user = getOne(queryWrapper);
        }catch (Exception e){
            log.error(e);
            throw new MyException(Constants.CODE_500,"系统错误");
        }
        return user;
    }

    // 获取当前角色的菜单列表
    private List<Menu> getRoleMenus(Integer roleId){
        List<Integer> menuIds = roleMenuMapper.getByRoleId(roleId);

        List<Menu> menus = menuService.getAll();//查出所有的菜单
        List<Menu> roleMenu = new ArrayList<>();
        //筛选所有用户角色的菜单
            for (Menu menu : menus) {
            if (menuIds.contains(menu.getId())) {
                roleMenu.add(menu);
            }
            List<Menu> children = menu.getChildren();
            children.removeIf(child -> ! menuIds.contains(child.getId()));
        }
        return roleMenu;
    }
}
